home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / ext / covers / lastfm.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  88 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from os import path
  5. from gi.repository import Soup
  6. from quodlibet.plugins.cover import CoverSourcePlugin, cover_dir
  7. from quodlibet.util.http import download_json
  8. from quodlibet.util.cover.http import HTTPDownloadMixin
  9. from quodlibet.util.path import escape_filename
  10.  
  11. class LastFMCover(CoverSourcePlugin, HTTPDownloadMixin):
  12.     PLUGIN_ID = 'lastfm-cover'
  13.     PLUGIN_NAME = _('LastFM cover source')
  14.     PLUGIN_DESC = _('Use LastFM database to fetch covers')
  15.     PLUGIN_VERSION = '1.0'
  16.     
  17.     def group_by(cls, song):
  18.         return song.album_key
  19.  
  20.     group_by = classmethod(group_by)
  21.     
  22.     def priority():
  23.         return 0.33
  24.  
  25.     priority = staticmethod(priority)
  26.     
  27.     def cover_path(self):
  28.         mbid = self.song.get('musicbrainz_albumid', None)
  29.         if mbid:
  30.             return path.join(cover_dir, escape_filename(mbid))
  31.         return None(LastFMCover, self).cover_path
  32.  
  33.     cover_path = property(cover_path)
  34.     
  35.     def url(self):
  36.         _url = 'http://ws.audioscrobbler.com/2.0?method=album.getinfo&' + 'api_key=107db6fd4c1c7f53b1526fafddab2c82&format=json&' + '&artist={artist}&album={album}&mbid={mbid}'
  37.         artist = Soup.URI.encode(self.song.get('artist', ''), None)
  38.         album = Soup.URI.encode(self.song.get('album', ''), None)
  39.         mbid = Soup.URI.encode(self.song.get('musicbrainz_albumid', ''), None)
  40.         if artist or album or mbid:
  41.             return _url.format(artist = artist, album = album, mbid = mbid)
  42.         return None
  43.  
  44.     url = property(url)
  45.     
  46.     def search(self):
  47.         if not self.url:
  48.             return self.emit('search-complete', [])
  49.         msg = None.Message.new('GET', self.url)
  50.         download_json(msg, self.cancellable, self.album_data, None)
  51.  
  52.     
  53.     def album_data(self, message, json, data = None):
  54.         if not json:
  55.             print_d('Server did not return valid JSON')
  56.             return self.emit('search-complete', [])
  57.         album = None.get('album', { })
  58.         if not album:
  59.             print_d('Album data is not available')
  60.             return self.emit('search-complete', [])
  61.         covers = None((lambda .0: pass)(album['image']))
  62.         result = []
  63.         for ck in ('mega', 'extralarge'):
  64.             if covers.get(ck):
  65.                 result.append({
  66.                     'artist': album['artist'],
  67.                     'album': album['name'],
  68.                     'cover': covers[ck] })
  69.                 continue
  70.         self.emit('search-complete', result)
  71.  
  72.     
  73.     def fetch_cover(self):
  74.         if not self.url:
  75.             return self.fail('Not enough data to get cover from LastFM')
  76.         
  77.         def search_complete(self, res):
  78.             self.disconnect(sci)
  79.             if res:
  80.                 self.download(Soup.Message.new('GET', res[0]['cover']))
  81.             else:
  82.                 return self.fail('No cover was found')
  83.  
  84.         sci = self.connect('search-complete', search_complete)
  85.         self.search()
  86.  
  87.  
  88.